package org.example.ding.repository;

import org.example.ding.entity.OrderRecord;
import org.example.ding.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 点餐记录数据访问层
 */
@Repository
public interface OrderRecordRepository extends JpaRepository<OrderRecord, Long> {

    /**
     * 根据用户查找点餐记录
     */
    List<OrderRecord> findByUser(User user);

    /**
     * 根据用户ID查找点餐记录
     */
    List<OrderRecord> findByUserId(Long userId);

    /**
     * 根据用户和日期查找点餐记录
     */
    List<OrderRecord> findByUserAndOrderDate(User user, LocalDate orderDate);

    /**
     * 根据用户ID和日期查找点餐记录
     */
    List<OrderRecord> findByUserIdAndOrderDate(Long userId, LocalDate orderDate);

    /**
     * 根据用户、日期和餐别查找点餐记录
     */
    List<OrderRecord> findByUserAndOrderDateAndMealType(User user, LocalDate orderDate, OrderRecord.MealType mealType);

    /**
     * 根据用户ID、日期和餐别查找点餐记录
     */
    List<OrderRecord> findByUserIdAndOrderDateAndMealType(Long userId, LocalDate orderDate, OrderRecord.MealType mealType);

    /**
     * 根据用户和日期范围查找点餐记录
     */
    List<OrderRecord> findByUserAndOrderDateBetween(User user, LocalDate startDate, LocalDate endDate);

    /**
     * 根据用户ID和日期范围查找点餐记录
     */
    List<OrderRecord> findByUserIdAndOrderDateBetween(Long userId, LocalDate startDate, LocalDate endDate);

    /**
     * 计算用户指定日期的总营养摄入
     */
    @Query("SELECT SUM(o.actualCalories), SUM(o.actualProtein), SUM(o.actualCarbs), SUM(o.actualFat) " +
           "FROM OrderRecord o WHERE o.user.id = :userId AND o.orderDate = :date")
    Object[] calculateDailyNutritionByUserId(@Param("userId") Long userId, @Param("date") LocalDate date);

    /**
     * 计算用户指定日期和餐别的营养摄入
     */
    @Query("SELECT SUM(o.actualCalories), SUM(o.actualProtein), SUM(o.actualCarbs), SUM(o.actualFat) " +
           "FROM OrderRecord o WHERE o.user.id = :userId AND o.orderDate = :date AND o.mealType = :mealType")
    Object[] calculateMealNutritionByUserId(@Param("userId") Long userId, @Param("date") LocalDate date, 
                                           @Param("mealType") OrderRecord.MealType mealType);

    /**
     * 获取用户最近N天的点餐记录
     */
    @Query("SELECT o FROM OrderRecord o WHERE o.user.id = :userId AND o.orderDate >= :startDate ORDER BY o.orderDate DESC, o.orderTime DESC")
    List<OrderRecord> findRecentOrdersByUserId(@Param("userId") Long userId, @Param("startDate") LocalDate startDate);

    /**
     * 统计用户总点餐次数
     */
    @Query("SELECT COUNT(o) FROM OrderRecord o WHERE o.user.id = :userId")
    Long countOrdersByUserId(@Param("userId") Long userId);

    /**
     * 统计用户指定日期范围内的点餐次数
     */
    @Query("SELECT COUNT(o) FROM OrderRecord o WHERE o.user.id = :userId AND o.orderDate BETWEEN :startDate AND :endDate")
    Long countOrdersByUserIdAndDateRange(@Param("userId") Long userId, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);

    /**
     * 获取用户最喜欢的菜品（按点餐次数排序）
     */
    @Query("SELECT o.dish.id, o.dish.name, COUNT(o) as orderCount " +
           "FROM OrderRecord o WHERE o.user.id = :userId " +
           "GROUP BY o.dish.id, o.dish.name ORDER BY orderCount DESC")
    List<Object[]> findFavoriteDishesById(@Param("userId") Long userId);

    /**
     * 获取用户指定餐别的点餐统计
     */
    @Query("SELECT o.mealType, COUNT(o), AVG(o.actualCalories) " +
           "FROM OrderRecord o WHERE o.user.id = :userId " +
           "GROUP BY o.mealType")
    List<Object[]> getMealTypeStatisticsByUserId(@Param("userId") Long userId);

    /**
     * 获取用户每日营养摄入趋势（最近N天）
     */
    @Query("SELECT o.orderDate, SUM(o.actualCalories), SUM(o.actualProtein), SUM(o.actualCarbs), SUM(o.actualFat) " +
           "FROM OrderRecord o WHERE o.user.id = :userId AND o.orderDate >= :startDate " +
           "GROUP BY o.orderDate ORDER BY o.orderDate")
    List<Object[]> getDailyNutritionTrendByUserId(@Param("userId") Long userId, @Param("startDate") LocalDate startDate);

    /**
     * 检查用户今日是否已点餐
     */
    @Query("SELECT COUNT(o) > 0 FROM OrderRecord o WHERE o.user.id = :userId AND o.orderDate = :date")
    boolean hasOrderedTodayByUserId(@Param("userId") Long userId, @Param("date") LocalDate date);

    /**
     * 检查用户今日指定餐别是否已点餐
     */
    @Query("SELECT COUNT(o) > 0 FROM OrderRecord o WHERE o.user.id = :userId AND o.orderDate = :date AND o.mealType = :mealType")
    boolean hasOrderedMealTodayByUserId(@Param("userId") Long userId, @Param("date") LocalDate date, @Param("mealType") OrderRecord.MealType mealType);

    /**
     * 删除用户指定日期的所有点餐记录
     */
    void deleteByUserIdAndOrderDate(Long userId, LocalDate orderDate);

    /**
     * 删除用户指定日期和餐别的点餐记录
     */
    void deleteByUserIdAndOrderDateAndMealType(Long userId, LocalDate orderDate, OrderRecord.MealType mealType);
}
